package com.linkstec.config;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.linkstec.util.RedisUtil;

/**
 * redis配置类
 *
 * @author linkage
 * @date 2022/11/30 15:12
 */
@SpringBootConfiguration
public class RedisClusterConfig implements CachingConfigurer {

    @Value("${spring.redis.key-prefix:MOT}")
    private String keyNameSpace;

    @Bean
    public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
//    FastJson2JsonRedisSerializer<Object> fastJson2JsonRedisSerializer =
//        new FastJson2JsonRedisSerializer<>(Object.class, StandardCharsets.UTF_8);
        // 默认UTF8编码
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(StandardCharsets.UTF_8);
        // 设置序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.setEnableTransactionSupport(false);
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringTemplate = new StringRedisTemplate();
        stringTemplate.setConnectionFactory(redisConnectionFactory);
        return stringTemplate;
    }

    @Bean
    public RedisUtil getRedisUtil(RedisTemplate<String, Object> redisTemplate) {
        return new RedisUtil(keyNameSpace, redisTemplate);
    }

    @Bean
    public BeanPostProcessor lettuceConnectionFactoryBeanProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                // 在 LettuceConnectionFactory 这个 Bean 初始化之后，设置 PipeliningFlushPolicy 为
                // flushOnClose
                if (bean instanceof LettuceConnectionFactory) {
                    LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) bean;
                    lettuceConnectionFactory
                            .setPipeliningFlushPolicy(LettuceConnection.PipeliningFlushPolicy.flushOnClose());
                    // 多谢评论区的[孤胆枪手](https://juejin.cn/user/2084329775180605) 的指正，我之前忘了放这个配置了
                    lettuceConnectionFactory.setShareNativeConnection(false);
                }
                return bean;
            }
        };
    }
}
